Skip to content

Conversation

@jenstroeger
Copy link
Contributor

Closes #5700

As discussed in the related issue, starting with comment #5700 (comment).

@github-actions

This comment has been minimized.

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! A couple nits.

@jenstroeger
Copy link
Contributor Author

Also, I’m not sure about your merge habits (do you squash the PR or not) so I’m just going to keep amending to the original commit so there’s just one commit to merge. Keeps the history clean.

@AlexWaygood
Copy link
Member

Also, I’m not sure about your merge habits (do you squash the PR or not) so I’m just going to keep amending to the original commit so there’s just one commit to merge. Keeps the history clean.

We always squash-merge at typeshed :) see our CONTRIBUTING guidelines: https://github.com/python/typeshed/blob/main/CONTRIBUTING.md#submitting-changes

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is turning out trickier than expected :)

_ExcInfoType: TypeAlias = None | bool | _SysExcInfoType | BaseException
_ArgsType: TypeAlias = tuple[object, ...] | Mapping[str, object]
_FilterType: TypeAlias = Filter | Callable[[LogRecord], bool]
_FilterType: TypeAlias = _SupportsFilter | Callable[[LogRecord], bool]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Turns out we should keep Filter here, based on the mypy_primer output. Sphinx uses # type: ignore when they define a custom filter with a weird filter() method. Including Filter here means that they don't get errors when adding the custom filter, only when defining it. Getting errors in only one place is better IMO.
  • On 3.12+, we should probably support the LogRecord return type here too.
Suggested change
_FilterType: TypeAlias = _SupportsFilter | Callable[[LogRecord], bool]
if sys.version_info >= (3, 12):
_FilterType: TypeAlias = Filter | _SupportsFilter | Callable[[LogRecord], bool | LogRecord]
else:
_FilterType: TypeAlias = Filter | _SupportsFilter | Callable[[LogRecord], bool]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit eed6015 🤓

@github-actions

This comment has been minimized.

@jenstroeger
Copy link
Contributor Author

jenstroeger commented Nov 13, 2023

I was thinking that by making _FilterType private we’ll have to then replicate it wherever a logging filter can be passed, see for example pytest issue pytest-dev/pytest#11610 🤔

@Akuli
Copy link
Collaborator

Akuli commented Nov 14, 2023

Pytest and whoever else needs to annotate "just some filter" can use logging._FilterType, even if it is named with an underscore in front.

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! One more nit.


if sys.version_info >= (3, 12):
class _SupportsFilter(Protocol):
def filter(self, record: LogRecord) -> bool | LogRecord: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the argument positional-only in the protocol, so if your class names it something else than Record, it will still match the protocol:

Suggested change
def filter(self, record: LogRecord) -> bool | LogRecord: ...
def filter(self, __record: LogRecord) -> bool | LogRecord: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the change in commit fe68d1a, but I’m still a little unclear: is the dunder __record a convention that the name is now “flexible”?

It doesn’t make the record arg (not capitalized Record) positional-only, though, that would require a / as well, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type-checkers special-case __dunder-named arguments and treat them as positional-only. This is a bit surprising, and we will soon be able to switch to the new / syntax.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is documented in PEP 484: https://peps.python.org/pep-0484/#positional-only-arguments

(PEP 484 predates PEP 570, which was only implemented in Python 3.8 -- typeshed still supports Python 3.7.)

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logging.Logger.addFilter

3 participants